<template>
  <div class="role-container">
    <!-- 左侧角色 -->
    <div class="left-wrapper">
      <div
        v-for="(item, index) in roleList"
        :key="item.roleId"
        class="role-item"
        :class="{active: activeIndex === index }"
        @click="changeRole(index)"
      >
        <div class="role-info">
          <svg-icon icon-class="user" class="icon" />
          {{ item.roleName }}
        </div>
        <div class="more">
          <svg-icon icon-class="more" name="more" />
        </div>
      </div>
      <el-button class="addBtn" size="mini" @click="$router.push('/roleAdd')">添加角色</el-button>
    </div>
    <!-- 右侧权限点 -->
    <div class="right-wrapper">
      <!-- tabs页签，可以切换显示 -->
      <el-tabs v-model="activeTabName">
        <el-tab-pane label="功能权限" name="perms">
          <div class="tree-wrapper">
            <div v-for="item in treeList" :key="item.id" class="tree-item">
              <div class="tree-title">{{ item.title }}</div>
              <el-tree
                ref="tree"
                :data="item.children"
                :props="{ label: 'title' }"
                show-checkbox
                default-expand-all
                node-key="id"
              />
            </div>
          </div>
        </el-tab-pane>
        <el-tab-pane :label="`成员(${roleUserList.length})`" name="users">
          <div class="user-wrapper">
            <el-table
              :data="roleUserList"
              style="width: 100%"
            >
              <el-table-column
                type="index"
                width="250"
                label="序号"
              />
              <el-table-column
                prop="name"
                label="员工姓名"
              />
              <el-table-column
                prop="userName"
                label="登录账号"
              />
            </el-table>
          </div>
        </el-tab-pane>
      </el-tabs>
    </div>
  </div></template>

<script>
import { getRoleListAPI, getTreeListAPI, getRoleDetailAPI, getRoleUserAPI } from '@/api/system'
export default {
  name: 'Role',
  data() {
    return {
      activeTabName: 'perms',
      roleUserList: [], // 当前角色下的成员列表
      roleList: [],
      activeIndex: 0,
      treeList: [], // 权限树形列表
      perms: [] // 当前角色权限点列表
    }
  },
  created() {
    Promise.all([
      this.getRoleList(),
      this.getTreeList()
    ])
      .then(() => {
        // 初始化时默认让第一行高亮，获取勾选的权限点
        this.getRoleDetail(this.roleList[0].roleId)
        // 初始化时获取第一个角色的所有用户
        this.getRoleUserList(this.roleList[0].roleId)
      })
  },
  methods: {
    // 封装请求方法
    async getRoleDetail(roleId) {
      const res = await getRoleDetailAPI(roleId)
      this.perms = res.data.perms
      // 高亮权限列表
      // 调用setCheckedKeys方法
      this.$refs.tree.forEach((tree, index) => {
        tree.setCheckedKeys(this.perms[index])
      })
    },
    async getRoleList() {
      const res = await getRoleListAPI()
      this.roleList = res.data
      console.log(res)
    },
    async getTreeList() {
      const res = await getTreeListAPI()
      this.treeList = res.data
      // 禁用
      // 目标：treeList里面的每一项以及嵌套的子项都添加一个disabled = true
      this.addDisabled(this.treeList)
    },
    // 递归处理函数
    addDisabled(treeList) {
      treeList.forEach(item => {
        item.disabled = true
        // 递归处理
        if (item.children) {
          this.addDisabled(item.children)
        }
      })
    },
    // 封装获取当前角色下的成员的方法
    async getRoleUserList(roleId) {
      const res = await getRoleUserAPI(roleId)
      this.roleUserList = res.data.rows
    },
    // 切换角色是获取roleId
    changeRole(idx) {
      this.activeIndex = idx
      // 获取当前角色下的权限点数据列表
      const roleId = this.roleList[idx].roleId
      // 1. 切换角色是获取最新的角色详情数据
      this.getRoleDetail(roleId)
      // 2. 获取当前角色下的成员数据列表
      this.getRoleUserList(roleId)
    }
  }
}
</script>

<style scoped lang="scss">
.role-container {
  display: flex;
  font-size: 14px;
  background-color: #fff;
  // padding:20px;
  .left-wrapper {
    width: 200px;
    border-right: 1px solid #e4e7ec;
    padding: 4px;
    text-align: center;

    .role-item {
      position: relative;
      height: 56px;
      display: flex;
      align-items: center;
      justify-content: space-between;
      padding: 0 20px;
      cursor: pointer;

      &.active {
        background: #edf5ff;
        color: #4770ff;
      }
    }

    .role-info {
      display: flex;
      align-items: center;

      .icon {
        margin-right: 10px;
      }
    }

    .more {
      display: flex;
      align-items: center;
    }
    .addBtn{
      width: 100%;
      margin-top: 20px;
    }
  }

  .right-wrapper {
    flex: 1;

    .tree-wrapper {
      display: flex;
      justify-content: space-between;

      .tree-item {
        flex: 1;
        border-right: 1px solid #e4e7ec;
        padding: 0px 4px;
        text-align: center;
        .tree-title {
          background: #f5f7fa;
          text-align: center;
          padding: 10px 0;
          margin-bottom: 12px;
        }
      }
    }

    ::v-deep .el-tabs__nav-wrap {
      padding-left: 20px;
    }

    .user-wrapper{
      padding:20px;
    }
  }
}
</style>
